package com.xyebank.ledger.service.common;

import com.xyebank.ledger.dao.deal.LApplicationInfMapper;
import com.xyebank.ledger.dao.deal.LPaymentInfMapper;
import com.xyebank.ledger.dao.deal.LRepaymentInfMapper;
import com.xyebank.ledger.db.entitys.LApplicationInf;
import com.xyebank.ledger.db.entitys.LPaymentInf;
import com.xyebank.ledger.db.entitys.LRepaymentInf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标准台帐３张表的ＤＡＯ
 */
@Component
public class UnionLedgerDAO {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    LApplicationInfMapper lApplicationInfMapper;

    @Autowired
    LPaymentInfMapper lPaymentInfMapper;

    @Autowired
    LRepaymentInfMapper lRepaymentInfMapper;

    @Resource(name = "ledgerRequiresNewTransactionTemplate")
    TransactionTemplate transactionTemplate;
    /**
     * 插入申请部分
     *
     * @return
     */
    public Integer insertLApplicationInf(final String title, final boolean isMig, final List<LApplicationInf> entitys, final List sourceList) {
        Object num = transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                int sucNum = 0;
                for (int i = 0; i < entitys.size(); i++) {
                    LApplicationInf nxCashPayment = entitys.get(i);
                    try {
                        sucNum += lApplicationInfMapper.insertSelective(nxCashPayment);
                    } catch (Exception e) {
                        if (isMig) {
                            logger.error("迁移{}插入失败, 原数据记录{},失败原因{}", title, sourceList.get(i), e.getMessage());
                        } else {
                            logger.error("同步{}插入失败,记录信息{},失败原因{}", title, sourceList.get(i), e.getMessage());
                        }
                    }
                }
                return sucNum;
            }
        });
        return (Integer) num;
    }



    /**
     * 插入放款部分
     *
     * @return
     */
    public Integer insertLPaymentInf(final String title, final boolean isMig, final List<LPaymentInf> entitys, final List sourceList) {
        Object num = transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                int sucNum = 0;
                for (int i = 0; i < entitys.size(); i++) {
                    LPaymentInf nxPayment = entitys.get(i);
                    try {
                        sucNum += lPaymentInfMapper.insertSelective(nxPayment);
                    } catch (Exception e) {
                        if (isMig) {
                            logger.error("迁移{}插入失败, 原数据记录{},失败原因{}", title, sourceList.get(i), e.getMessage());
                        } else {
                            logger.error("同步{}插入失败,记录信息{},失败原因{}", title, sourceList.get(i), e.getMessage());
                        }
                    }
                }
                return sucNum;
            }
        });
        return (Integer) num;
    }


    /**
     * 插入还款部分
     *
     * @return
     */
    public Integer insertLRePaymentInf(final String title, final boolean isMig, final List<LRepaymentInf> entitys, final List sourceList) {
        Object num = transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus status) {
                int sucNum = 0;
                for (int i = 0; i < entitys.size(); i++) {
                    LRepaymentInf nxPayment = entitys.get(i);
                    try {
                        sucNum += lRepaymentInfMapper.insertSelective(nxPayment);
                    } catch (Exception e) {
                        if (isMig) {
                            logger.error("迁移{}插入失败, 原数据记录{},失败原因{}", title, sourceList.get(i), e.getMessage());
                        } else {
                            logger.error("同步{}插入失败,记录信息{},失败原因{}", title, sourceList.get(i), e.getMessage());
                        }
                    }
                }
                return sucNum;
            }
        });
        return (Integer) num;
    }
}
